<template>
  <div class="login">
    <div class="login-container">
      <el-tabs v-model="activeName">
        <el-tab-pane label="LOGIN" name="login">
          <el-form
            ref="user1"
            :model="user"
            :rules="rules1"
            label-position="top"
            class="form-container"
          >
            <el-form-item label="账号" prop="username">
              <el-input
                v-model="user.username"
                placeholder="请输入您的账号"
                prefix-icon="el-icon-user"
                maxlength="20"
                show-word-limit
              ></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input
                v-model="user.password"
                placeholder="请输入您的密码"
                prefix-icon="el-icon-lock"
                type="password"
              ></el-input>
            </el-form-item>

            <el-form-item prop="role" class="role-container">
              <el-radio-group v-model="user.role">
                <el-radio label="用户"></el-radio>
                <el-radio label="管理员"></el-radio>
              </el-radio-group>
            </el-form-item>

            <div class="button-container">
              <el-button type="primary" @click="submitForm('user1')" round>
                <i class="el-icon-user"></i>登录
              </el-button>
              <el-button @click="resetForm('user1')" round>
                <i class="el-icon-lock"></i>重置
              </el-button>
              <el-button type="primary" @click="toFaceLogin" round>
                <i class="el-icon-camera"></i>人脸识别登录
              </el-button>
            </div>
          </el-form>
        </el-tab-pane>

        <el-tab-pane label="REGISTER" name="register">
          <el-form
            ref="user"
            :model="user"
            :rules="rules2"
            label-position="top"
            class="form-container"
          >
            <el-form-item label="账号" prop="username">
              <el-input
                v-model="user.username"
                placeholder="请输入您的账号"
                prefix-icon="el-icon-user"
                maxlength="20"
                show-word-limit
              ></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input
                v-model="user.password"
                placeholder="请输入您的密码"
                prefix-icon="el-icon-lock"
                type="password"
              ></el-input>
            </el-form-item>

            <el-form-item prop="role" class="role-container">
              <el-radio-group v-model="user.role">
                <el-radio label="用户"></el-radio>
                <el-radio label="管理员"></el-radio>
              </el-radio-group>
            </el-form-item>

            <div class="button-container">
              <el-button type="primary" @click="registerForm('user')" round>
                <i class="el-icon-user"></i>注册
              </el-button>
              <el-button @click="resetForm('user')" round>
                <i class="el-icon-lock"></i>重置
              </el-button>
              <el-button type="primary" @click="toPhotoCapture" round>
                <i class="el-icon-camera"></i>人脸注册
              </el-button>
            </div>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { LoginAPI, RegisterAPI, BookRturnSellctAPI } from "@/request/api";
export default {
  data() {
    return {
      tabPosition: "left",
      labelPosition: "right",
      isRequired: true,
      activeName: "login",
      user: {
        username: "",
        password: "",
        role: "",
      },
      rules1: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 1,
            max: 12,
            message: "长度在 1 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        role: [{ required: true, message: "请选择身份", trigger: "change" }],
      },
      rules2: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 1,
            max: 12,
            message: "长度在 1 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        role: [{ required: true, message: "请选择身份", trigger: "change" }],
      },
    };
  },
  methods: {
    ...mapMutations(["saveUsername","saverole"]),
    //登录确认
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //登录
          let users = {};
          users.username = this.user.username;
          users.password = this.user.password;
          users.role = this.user.role === "用户" ? 2 : 1;
          // console.log(users);
          let res = await LoginAPI(users);
          // console.log(res);

          //登陆成功
          if (res.status == 0) {
            this.$message({
              message: res.message,
              type: "success",
            });
            //保存token
            localStorage.setItem("book_token", res.token);
            //保存用户名
            this.saveUsername(this.user.username);
            //保存权限
            this.saverole(this.user.role)
            //清空内容
            this.user = {};
            //催还提示
            this.Reminder();
            this.$router.push("/home");
          } else {
            this.$message.error(res.message);
          }
        } else {
          this.$message({
            message: "信息未填写完整!",
            type: "warning",
          });
          return false;
        }
      });
    },
    //催还提示
    async Reminder(list) {
      let res =await BookRturnSellctAPI({currentPage:1,size:8});
      let j =0
      for (let i=0;i<res.data.length;i++) {
        if(res.data[i].urgerepayment === 0){
          this.$notify({
          title: '催还消息',
          message: `管理员催还了《${res.data[i].bookname}》`,
          type: 'warning',
          offset: 80*j
        });
        j++
        }
      }
    },
    //注册确认
    registerForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //注册
          let users = {};
          users.username = this.user.username;
          users.password = this.user.password;
          users.role = this.user.role === "用户" ? 2 : 1;
          console.log(users);
          let res = await RegisterAPI(users);
          console.log(res);

          if (res.status == 0) {
            //注册成功!
            this.$message({
              message: res.message,
              type: "success",
            });
            this.activeName = "login"; // 切换到登录标签页
          } else {
            this.$message.error(res.message);
          }
        } else {
          this.$message({
            message: "信息未填写完整!",
            type: "warning",
          });
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    toFaceLogin() {
      this.$router.push('/facelogin');
    },

    toPhotoCapture() {
    this.$router.push('/photocapture');
  }
  },
};
</script>

<style lang="less" scoped>
// 添加移动端视口设置
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap');

.login {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  background-image: url("../assets/img/LoginBackground.jpg");
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;

  .login-container {
    width: 90%;
    max-width: 335px; // iPhone SE 宽度的90%
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

    :deep(.el-tabs__header) {
      margin-bottom: 20px;
    }

    :deep(.el-tabs__nav-wrap) {
      padding: 0 10px;
    }

    :deep(.el-tabs__item) {
      font-size: 16px;
      padding: 0 15px;
      height: 36px;
      line-height: 36px;
      color: #606266;

      &.is-active {
        color: #409EFF;
      }
    }
  }

  .form-container {
    .el-form-item {
      margin-bottom: 15px;
    }

    :deep(.el-form-item__label) {
      padding: 0;
      line-height: 24px;
      font-size: 14px;
      color: #606266;
    }

    :deep(.el-input) {
      .el-input__inner {
        height: 40px;
        line-height: 40px;
        border-radius: 8px;
      }
    }

    .role-container {
      margin: 15px 0;
      
      :deep(.el-radio-group) {
        display: flex;
        justify-content: space-around;
        width: 100%;

        .el-radio {
          margin-right: 0;
        }
      }
    }

    .button-container {
      display: flex;
      flex-direction: column;
      gap: 10px;
      margin-top: 20px;

      .el-button {
        width: 100%;
        height: 40px;
        margin: 0;
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: center;

        i {
          margin-right: 5px;
        }
      }
    }
  }
}

// 添加移动端适配样式
@media screen and (max-width: 375px) {
  .login {
    .login-container {
      width: 95%;
      padding: 15px;
    }

    .form-container {
      :deep(.el-form-item__label) {
        font-size: 13px;
      }

      :deep(.el-input__inner) {
        font-size: 13px;
      }

      .button-container {
        .el-button {
          font-size: 13px;
        }
      }
    }
  }
}
.login .form-container .button-container .el-button[data-v-26084dc2] {
    width: 100%;
    height: 40px;
    margin: 0;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    left: 0px;
  }
</style>

